#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""docstring
"""

import sys
import getpass
from flask import Flask, current_app
from flask.ext.script import Server, Shell, Manager, Command, prompt_bool
from werkzeug.contrib.fixers import ProxyFix

from planet.extensions import db, cache
from planet import create_app
from planet.account.models import User

app = create_app('config.conf')
app.wsgi_app = ProxyFix(app.wsgi_app)

manager = Manager(app)
manager.add_command("runserver", Server('127.0.0.1', port=5002))


def _make_context():
    return dict(db=db)
manager.add_command("shell", Shell(make_context=_make_context))


@manager.command
def createall():
    db.create_all()


@manager.command
def dropall():
    if prompt_bool:
        db.drop_all()


@manager.command
def create_superuser():
    cmd = raw_input('Create superuser?(Y/n): ')
    if cmd == 'n':
        sys.exit(1)
    username = raw_input('username: ')
    email = raw_input('email: ')
    password = getpass.getpass('password: ')
    user = User(
        id=1,
        username=username,
        email=email,
        password=password,
        role=1,
        active=True)
    db.session.add(user)
    db.session.commit()

if __name__ == "__main__":
    manager.run()
